<?php

/**
 * descript:
 * User: blue
 * Date: 2018/9/13 16:47
 */

namespace app\library\base;

use app\library\except\ParmasException;
use think\Exception;
use think\Validate;
use think\facade\Log;

class BaseValidate extends Validate
{
    /**
     * 通用验证方法
     * @param array $data
     * @throws ParmasException
     */
    public function goCheck(array $data,$scene=''){
        $scenceArr=['post','put','patch','get','delete','regist','login'];
        if(!is_array($data))throw new ParmasException('参数不是正常的数组');
        if($scene){
            //判断是不是合适的场景动词
            if(in_array(strtolower($scene),$scenceArr)){
                $check=$this->scene($scene)->batch()->check($data);
            }else{
                Log::write($scene.'不是正确的验证场景动词');
                throw new Exception();
            }
        }else{
            $check=$this->batch()->check($data);
        }
        if(!$check){
            $message=json_encode($this->getError(),JSON_UNESCAPED_UNICODE);
            throw new ParmasException($message);
        }
    }

    /**
     * 判断是不是正整数
     * @param $value 验证数据的值
     * @param $rule
     * @param array $data 全部数据
     */
    protected function positive($value){
        if(is_numeric($value)&&is_int($value+0)&&($value+0)>=0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 验证ids 字符串1,2,3
     * @param $value
     * @return bool
     */
    protected  function  checkIds($value){
        $arr=explode(',',$value);
        if(empty($value)) return false;
        foreach ($arr as $key=>$v){
            $res=$this->positive($v);
            if(!$res) return false;
        }
        return true;
    }


}